# encoding: utf-8

require 'rubygems'
require 'test/unit'
require File.dirname(__FILE__) + '/../../../lib/capevaluation'

class Test_Service_Example < Test::Unit::TestCase

  def setup
    @config = CaiQiu::Support::Config::get_json(File.dirname(__FILE__) +
                                                    '/../../../etc/ws.json')
    CaiQiu::Core::Launch::load_config(@config)
  end

  def teardown
  end

#BEGIN
# INSERT INTO `example` (`value`) VALUES ('foo')
# SELECT * FROM `example` WHERE (`id` = 1) LIMIT 1
#COMMIT
  def test_002_add_example
    Capevaluation::Service::Example.add_example('foo', 0)
  end

# SELECT `id`, `value` FROM `example` WHERE (`value` = 'foo')
# explain SELECT `id`, `value` FROM `example` WHERE (`value` = 'foo') ;
# mysql> explain SELECT `id`, `value` FROM `example` WHERE (`value` = 'foo') ;
# +----+-------------+---------+------+---------------+------+---------+------+------+-------------+
# | id | select_type | table   | type | possible_keys | key  | key_len | ref  | rows | Extra       |
# +----+-------------+---------+------+---------------+------+---------+------+------+-------------+
# |  1 | SIMPLE      | example | ALL  | NULL          | NULL | NULL    | NULL |    1 | Using where |
# +----+-------------+---------+------+---------------+------+---------+------+------+-------------+
# 1 row in set (0.02 sec)

  def test_003_get_example_by_value
    example_list = Capevaluation::Service::Example.get_example_by_value('foo')
    example_list.each do |example|
      puts example.inspect
    end
  end

#BEGIN
# UPDATE `example` SET `value` = 'bar' WHERE (`id` = 4) LIMIT 1
#COMMIT
  def test_004_mod_example
    example_list = Capevaluation::Service::Example.get_example_by_value('foo')
    example_list.each do |example|
      example[:value] = 'bar'
      Capevaluation::Service::Example.mod_example(example)
    end
  end

#BEGIN
#  SELECT `id`, `value`, `status` FROM `example` WHERE (`id` = 2) LIMIT 1
#  UPDATE `example` SET `value` = 'trans' WHERE (`id` = 2) LIMIT 1
#  SELECT `id`, `value`, `status` FROM `example` WHERE (`id` = 3) LIMIT 1
#  UPDATE `example` SET `value` = 'trans' WHERE (`id` = 3) LIMIT 1
#COMMIT
  def test_005_trans_mod_examples
    example_list = Capevaluation::Service::Example.get_example_by_value('foo')
    example_list.each do |example|
      example[:value] = "trans#{Time.now.to_s}"
    end
    Capevaluation::Service::Example.trans_mod_examples(example_list)
  end

#BEGIN
# SELECT * FROM `example` WHERE (`id` = 5) LIMIT 1 FOR UPDATE
# UPDATE `example` SET `value` = 'lock' WHERE (`id` = 5) LIMIT 1
#COMMIT
  def test_006_mod_value_by_status
    example_list = Capevaluation::Service::Example.get_example_by_value('foo')
    example_list.each do |example|
      example[:value] = 'trans'
      Capevaluation::Service::Example.trans_mod_value_by_status(example)
    end
  end

#mysql> explain SELECT `id`, `value`, `status` FROM `example` ;
#+----+-------------+---------+------+---------------+------+---------+------+------+-------+
#| id | select_type | table   | type | possible_keys | key  | key_len | ref  | rows | Extra |
#+----+-------------+---------+------+---------------+------+---------+------+------+-------+
#|  1 | SIMPLE      | example | ALL  | NULL          | NULL | NULL    | NULL |    5 | NULL  |
#+----+-------------+---------+------+---------------+------+---------+------+------+-------+
  def test_007_list_examples
    example_list = Capevaluation::Service::Example.list_examples
    puts example_list.inspect
  end

end
